package com.iams.manage.service.impl;

import java.util.List;

import com.iams.common.utils.DateUtils;
import com.iams.common.utils.SecurityUtils;
import com.iams.common.utils.uuid.UUID;
import com.iams.manage.validator.ArchiveFilingApprovalValidator;
import com.iams.system.service.ISysUserService;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iams.manage.mapper.ArchiveFilingApprovalMapper;
import com.iams.manage.domain.ArchiveFilingApproval;
import com.iams.manage.service.IArchiveFilingApprovalService;

import org.activiti.api.process.runtime.ProcessRuntime;

/**
 * 归档申请Service业务层处理
 * 
 * @author LiuTao
 * @date 2025-03-26
 */
@Service
public class ArchiveFilingApprovalServiceImpl implements IArchiveFilingApprovalService 
{
    @Autowired
    private ArchiveFilingApprovalMapper archiveFilingApprovalMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ArchiveFilingApprovalValidator archiveFilingApprovalValidator;

    @Autowired
    private ProcessRuntime processRuntime;
    /**
     * 查询归档申请
     * 
     * @param id 归档申请主键
     * @return 归档申请
     */
    @Override
    public ArchiveFilingApproval selectArchiveFilingApprovalById(Long id)
    {
        return archiveFilingApprovalMapper.selectArchiveFilingApprovalById(id);
    }

    /**
     * 查询归档申请列表
     * 
     * @param archiveFilingApproval 归档申请
     * @return 归档申请
     */
    @Override
    public List<ArchiveFilingApproval> selectArchiveFilingApprovalList(ArchiveFilingApproval archiveFilingApproval)
    {
        return archiveFilingApprovalMapper.selectArchiveFilingApprovalList(archiveFilingApproval);
    }

    /**
     * 新增归档申请
     * 
     * @param archiveFilingApproval 归档申请
     * @return 结果
     */
    @Override
    public int insertArchiveFilingApproval(ArchiveFilingApproval archiveFilingApproval)
    {
        archiveFilingApproval.setDeptName(sysUserService.selectUserById(SecurityUtils.getLoginUser().getUserId()).getDept().getDeptName());

        archiveFilingApprovalValidator.validate(archiveFilingApproval);
        archiveFilingApproval.setCreateTime(DateUtils.getNowDate());
        archiveFilingApproval.setStatus("待审批");
        if (archiveFilingApproval.getProcessName() == null)
        {
            archiveFilingApproval.setProcessName("ArchiveFiling");
        }

        String businessKey = UUID.randomUUID().toString();

        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey(archiveFilingApproval.getProcessName())
                .withBusinessKey(businessKey)
                .withName(archiveFilingApproval.getUserName() + "档案归档申请")
                .build());

        archiveFilingApproval.setInstanceId(processInstance.getId());
        archiveFilingApproval.setInstanceName(processInstance.getName());

        archiveFilingApproval.setBusinessKey(businessKey);

        return archiveFilingApprovalMapper.insertArchiveFilingApproval(archiveFilingApproval);
    }

    /**
     * 修改归档申请
     * 
     * @param archiveFilingApproval 归档申请
     * @return 结果
     */
    @Override
    public int updateArchiveFilingApproval(ArchiveFilingApproval archiveFilingApproval)
    {
        return archiveFilingApprovalMapper.updateArchiveFilingApproval(archiveFilingApproval);
    }

    /**
     * 批量删除归档申请
     * 
     * @param ids 需要删除的归档申请主键
     * @return 结果
     */
    @Override
    public int deleteArchiveFilingApprovalByIds(Long[] ids)
    {
        return archiveFilingApprovalMapper.deleteArchiveFilingApprovalByIds(ids);
    }

    /**
     * 删除归档申请信息
     * 
     * @param id 归档申请主键
     * @return 结果
     */
    @Override
    public int deleteArchiveFilingApprovalById(Long id)
    {
        return archiveFilingApprovalMapper.deleteArchiveFilingApprovalById(id);
    }

    @Override
    public ArchiveFilingApproval selectArchiveFilingApprovalByBusinessKey(String businessKey)
    {
        return archiveFilingApprovalMapper.selectArchiveFilingApprovalByBusinessKey(businessKey);
    }
}
